package com.xy.mvvmdemo

import android.annotation.SuppressLint
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.os.PersistableBundle
import android.util.ArrayMap
import android.util.Log
import android.view.View
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.tencent.mmkv.MMKV
import com.xy.mvvmdemo.bean.LiveDataBus
import com.xy.mvvmdemo.bean.UserBean
import java.util.HashMap


/**
 * @ClassName Activity2
 * @Author xy
 * @Date 2021/7/9 10:45
 * @Description TODO
 */
class Activity2 :AppCompatActivity() {
    @SuppressLint("CommitPrefEdits")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.acctivity2)
//        LiveDataBusBeta.getInstance().with("Activity2",).observe(this,{
            LiveDataBus.getInstance().with("Activity2").observe(this, {
                Toast.makeText(this, it.toString(), Toast.LENGTH_SHORT).show()
            })
        val sp = getSharedPreferences("", MODE_PRIVATE)
        val edit = sp.edit()
        edit.putString("ceShi", "")
        edit.apply()
//        System.arraycopy()
        val rootDir = MMKV.initialize(this,"test")
        println("mmkv root: $rootDir")

        val kv = MMKV.defaultMMKV()
        kv.putString("string","Hello World!")
        val str = kv.getString("string","")
        Toast.makeText(this,str,Toast.LENGTH_SHORT).show()
        var intent = Intent()
        intent.putExtra("1",1)
        intent.putExtra("2","2")
        var bundle = Bundle()
        bundle.putInt("1",1)
        intent.putExtras(bundle)
        var map = HashMap<String,String>()


    }

    fun send(view: View) {
        when(view.id){
            R.id.send -> {
//                LiveDataBusBeta.getInstance().with("MainActivity").value = "来自于Activity2 的问候"
                LiveDataBus.getInstance().with("MainActivity").value = "来自于Activity2 的问候"
            }
            R.id.change -> {
                var bean = UserBean()
                var classUserBean = bean::class.java
                var age = classUserBean.getDeclaredField("age") //获取类总私有属性
                age.isAccessible = true
                age.set(bean, 20) //get私有属性设置
                Toast.makeText(this, bean.isChild(), Toast.LENGTH_SHORT).show()
            }

            R.id.change1 -> {
                var cls = UserBean::class.java  //反射获取实体类方式一
//                 cls = Class.forName("com.xy.mvvmdemo.bean.UserBean") as Class<UserBean>  //反射获取实体类方式二
                val any = cls.newInstance()
                val declaredMethod = cls.getDeclaredMethod("isChild", Int::class.java) //获取类中的方法
                val declaredField = cls.getDeclaredField("name") //获取属性字段
                declaredField.isAccessible = true
                declaredField.set(any,"小张")

                declaredMethod.isAccessible = true //设置为可以访问
                var str = declaredMethod.invoke(any, 30) as String//执行私有方法
                Toast.makeText(this, str+"-----${cls.name}", Toast.LENGTH_SHORT).show()

            }
            R.id.jumpToActivity3->{
                startActivity(Intent(this,Activity3::class.java))
            }
        }

    }

    override fun onSaveInstanceState(outState: Bundle, outPersistentState: PersistableBundle) {
        super.onSaveInstanceState(outState, outPersistentState)
        Log.i("TAG", "onSaveInstanceState: ")
    }


}